package com.archive.manage.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.archive.common.core.domain.model.LoginUser;
import com.archive.common.utils.DateUtils;
import com.archive.common.utils.SecurityUtils;
import com.archive.common.utils.uuid.UUIDUtils;
import com.archive.manage.domain.StuArchive;
import com.archive.manage.domain.StuArchiveArchiveLog;
import com.archive.manage.domain.Vo.StuArchiveVo;
import com.archive.manage.domain.entity.ArchiveCreateResponse;
import com.archive.manage.domain.entity.ArchiveGetResponse;
import com.archive.manage.mapper.StuArchiveArchiveLogMapper;
import com.archive.manage.service.IBlockchainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.archive.manage.mapper.ArchiveLogMapper;
import com.archive.manage.domain.ArchiveLog;
import com.archive.manage.service.IArchiveLogService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 档案日志管理Service业务层处理
 * 
 * @author marioduan
 * @date 2025-04-03
 */
@Service
public class ArchiveLogServiceImpl implements IArchiveLogService 
{
    @Autowired
    private ArchiveLogMapper archiveLogMapper;

    @Autowired
    private StuArchiveArchiveLogMapper stuArchiveArchiveLogMapper;

    private IBlockchainService blockchainService = new BlockchainServiceImpl();
    /**
     * 查询档案日志管理
     * 
     * @param logId 档案日志管理主键
     * @return 档案日志管理
     */
    @Override
    public ArchiveLog selectArchiveLogByLogId(Long logId)
    {
        return archiveLogMapper.selectArchiveLogByLogId(logId);
    }

    /**
     * 查询档案日志管理列表
     * 
     * @param archiveLog 档案日志管理
     * @return 档案日志管理
     */
    @Override
    public List<ArchiveLog> selectArchiveLogList(ArchiveLog archiveLog)
    {
        return archiveLogMapper.selectArchiveLogList(archiveLog);
    }

    /**
     * 新增档案日志管理
     * 
     * @param archiveLog 档案日志管理
     * @return 结果
     */
    @Override
    public int insertArchiveLog(ArchiveLog archiveLog)
    {
        return archiveLogMapper.insertArchiveLog(archiveLog);
    }

    /**
     * 修改档案日志管理
     * 
     * @param archiveLog 档案日志管理
     * @return 结果
     */
    @Override
    public int updateArchiveLog(ArchiveLog archiveLog)
    {
        return archiveLogMapper.updateArchiveLog(archiveLog);
    }

    /**
     * 批量删除档案日志管理
     * 
     * @param logIds 需要删除的档案日志管理主键
     * @return 结果
     */
    @Override
    public int deleteArchiveLogByLogIds(Long[] logIds)
    {
        return archiveLogMapper.deleteArchiveLogByLogIds(logIds);
    }

    /**
     * 删除档案日志管理信息
     * 
     * @param logId 档案日志管理主键
     * @return 结果
     */
    @Override
    public int deleteArchiveLogByLogId(Long logId)
    {
        return archiveLogMapper.deleteArchiveLogByLogId(logId);
    }

    /**
     * 根据学号查询档案日志管理列表
     *
     * @param studentId 学号
     * @return 档案日志管理集合
     */
    @Override
    public List<ArchiveLog> selectArchiveLogByStudentId(String studentId) {
        return archiveLogMapper.selectArchiveLogByStudentId(studentId);
    }

    /**
     * 生成档案日志 上链
     * @param stuArchiveVo
     */
    @Override
    @Transactional
    public void archiveLog(StuArchiveVo stuArchiveVo, Long operationType, String trHash){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ArchiveLog archiveLog = new ArchiveLog();
        //模拟获取哈希值
        String logHash = UUIDUtils.getUUID();
        archiveLog.setLogHash(logHash);
        //操作类型
        archiveLog.setOperationType(operationType);
        //请求参数
        archiveLog.setOperationParam(JSON.toJSONString(stuArchiveVo));
        //操作人ID
        archiveLog.setOperatorId(loginUser.getUserId());
        //操作人姓名
        archiveLog.setOperatorName(loginUser.getUser().getUserName());
        //操作人IP
        archiveLog.setOperationIp(loginUser.getIpaddr());
        //操作时间
        archiveLog.setOperationTime(DateUtils.getNowDate());
        //链上哈希值
        archiveLog.setTransactionHash(trHash);
        //存入日志表
        archiveLogMapper.insertArchiveLog(archiveLog);
        //保存档案日志关联表
        StuArchiveArchiveLog stuArchiveArchiveLog = new StuArchiveArchiveLog(stuArchiveVo.getStudentId(),logHash);
        stuArchiveArchiveLogMapper.insertStuArchiveArchiveLog(stuArchiveArchiveLog);
    }

    /**
     * 生成档案日志不上链
     * @param stuArchiveVo
     */
    @Override
    @Transactional
    public void archiveLog(StuArchiveVo stuArchiveVo, Long operationType){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ArchiveLog archiveLog = new ArchiveLog();
        //模拟获取哈希值
        String logHash = UUIDUtils.getUUID();
        archiveLog.setLogHash(logHash);
        //操作类型
        archiveLog.setOperationType(operationType);
        //请求参数
        archiveLog.setOperationParam(JSON.toJSONString(stuArchiveVo));
        //操作人ID
        archiveLog.setOperatorId(loginUser.getUserId());
        //操作人姓名
        archiveLog.setOperatorName(loginUser.getUser().getUserName());
        //操作人IP
        archiveLog.setOperationIp(loginUser.getIpaddr());
        //操作时间
        archiveLog.setOperationTime(DateUtils.getNowDate());
        //存入日志表
        archiveLogMapper.insertArchiveLog(archiveLog);
        //保存档案日志关联表
        StuArchiveArchiveLog stuArchiveArchiveLog = new StuArchiveArchiveLog(stuArchiveVo.getStudentId(),logHash);
        stuArchiveArchiveLogMapper.insertStuArchiveArchiveLog(stuArchiveArchiveLog);
    }

    @Override
    @Transactional
    public void batchExportArchiveLog(String[] studentIds, Long operationType) throws Exception {
        List<ArchiveLog> archiveLogList = new ArrayList<>();
        List<StuArchiveArchiveLog> stuArchiveArchiveLogs = new ArrayList<>();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        for (int i = 0; i < studentIds.length; i++) {
            StuArchive stuArchive = new StuArchive();
            stuArchive.setStudentId(studentIds[i]);
            ArchiveLog archiveLog = new ArchiveLog();
            //模拟获取哈希值
            String logHash = UUIDUtils.getUUID();
            archiveLog.setLogHash(logHash);
            //操作类型
            archiveLog.setOperationType(operationType);
            //请求参数
            archiveLog.setOperationParam(JSON.toJSONString(stuArchive));
            //操作人ID
            archiveLog.setOperatorId(loginUser.getUserId());
            //操作人姓名
            archiveLog.setOperatorName(loginUser.getUser().getUserName());
            //操作人IP
            archiveLog.setOperationIp(loginUser.getIpaddr());
            //操作时间
            archiveLog.setOperationTime(DateUtils.getNowDate());
            archiveLogList.add(archiveLog);
            //批量保存档案日志关联表
            StuArchiveArchiveLog stuArchiveArchiveLog = new StuArchiveArchiveLog(studentIds[i],logHash);
            stuArchiveArchiveLogs.add(stuArchiveArchiveLog);
        }
        //批量保存档案日志操作
        archiveLogMapper.batchInsertArchiveLogByStudentIds(archiveLogList);
        //批量保存档案日志关联表
        stuArchiveArchiveLogMapper.batchInsertStuArchiveArchiveLog(stuArchiveArchiveLogs);
    }

    @Override
    @Transactional
    public void batchDeleteArchiveLog(String[] studentIds, Long operationType) throws Exception {
        List<ArchiveLog> archiveLogList = new ArrayList<>();
        List<StuArchiveArchiveLog> stuArchiveArchiveLogs = new ArrayList<>();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        for (int i = 0; i < studentIds.length; i++) {
            StuArchive stuArchive = new StuArchive();
            stuArchive.setStudentId(studentIds[i]);
            ArchiveLog archiveLog = new ArchiveLog();
            //模拟获取哈希值
            String logHash = UUIDUtils.getUUID();
            archiveLog.setLogHash(logHash);
            //操作类型
            archiveLog.setOperationType(operationType);
            //请求参数
            archiveLog.setOperationParam(JSON.toJSONString(stuArchive));
            //操作人ID
            archiveLog.setOperatorId(loginUser.getUserId());
            //操作人姓名
            archiveLog.setOperatorName(loginUser.getUser().getUserName());
            //操作人IP
            archiveLog.setOperationIp(loginUser.getIpaddr());
            //操作时间
            archiveLog.setOperationTime(DateUtils.getNowDate());
            //批量链上删除操作并记录日志
            ArchiveGetResponse archiveGetResponse = blockchainService.deleteArchive("studentarchive", "channel3", "org1", studentIds[i]);
            archiveLog.setTransactionHash(archiveGetResponse.getTransactionID());

            archiveLogList.add(archiveLog);
            //批量保存档案日志关联表
            StuArchiveArchiveLog stuArchiveArchiveLog = new StuArchiveArchiveLog(studentIds[i],logHash);
            stuArchiveArchiveLogs.add(stuArchiveArchiveLog);
        }
        //批量保存删除档案日志操作
        archiveLogMapper.batchInsertArchiveLogByStudentIds(archiveLogList);
        //批量保存档案日志关联表
        stuArchiveArchiveLogMapper.batchInsertStuArchiveArchiveLog(stuArchiveArchiveLogs);
    }

    @Override
    @Transactional
    public void batchInsertArchiveLog(List<StuArchive> stuArchiveList, Long operationType) throws Exception {
        List<ArchiveLog> archiveLogList = new ArrayList<>();
        List<StuArchiveArchiveLog> stuArchiveArchiveLogs = new ArrayList<>();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        for (StuArchive stuArchive : stuArchiveList) {
            ArchiveLog archiveLog = new ArchiveLog();
            //模拟获取哈希值
            String logHash = UUIDUtils.getUUID();
            archiveLog.setLogHash(logHash);
            //操作类型
            archiveLog.setOperationType(operationType);
            //请求参数
            archiveLog.setOperationParam(JSON.toJSONString(stuArchive));
            //操作人ID
            archiveLog.setOperatorId(loginUser.getUserId());
            //操作人姓名
            archiveLog.setOperatorName(loginUser.getUser().getUserName());
            //操作人IP
            archiveLog.setOperationIp(loginUser.getIpaddr());
            //操作时间
            archiveLog.setOperationTime(DateUtils.getNowDate());
            //循环新增上链
            //TODO:目前只上链了档案表(简历表,班级信息表等没有上链)
            ArchiveCreateResponse archiveResp = blockchainService.createArchive("studentarchive", "channel3", "org1", "zhangsan", stuArchive);
            //TransactionID
            archiveLog.setTransactionHash(archiveResp.getTransactionID());
            archiveLogList.add(archiveLog);
            //批量保存档案日志关联表
            StuArchiveArchiveLog stuArchiveArchiveLog = new StuArchiveArchiveLog(stuArchive.getStudentId(),logHash);
            stuArchiveArchiveLogs.add(stuArchiveArchiveLog);
        }
        //批量保存档案日志操作
        archiveLogMapper.batchInsertArchiveLogByStudentIds(archiveLogList);
        //批量保存档案日志关联表
        stuArchiveArchiveLogMapper.batchInsertStuArchiveArchiveLog(stuArchiveArchiveLogs);
    }
}
